#!/usr/bin/env bash

function init() {
    master=$1

    if [[ "$master" != "0" ]]; then
        echo "Begin to initiate databases..."
        for((x = 0; x <= ${master}-1; x++))
        do
            cat <<EOF | mysql -uapollo -pApollo2017!
drop database if exists master_${x};
create database master_${x}
  default char set utf8mb4
  default collate utf8mb4_general_ci;
EOF
            if [[ -f /work/module/maya/schema/init.sql ]]; then
                mysql -uapollo -pApollo2017! -D master_${x} < /work/module/maya/schema/init.sql
            fi
        done
        echo "Finished initiating databases..."
    fi
}

# Clean databases
function clean() {
    master=$1

    if [[ "$master" != "0" ]]; then
        echo "Begin to delete database..."
        for((x = 0; x <= ${master}-1; x++))
        do
            cat <<EOF | mysql -uapollo -pApollo2017!
drop database if exists master_${x};
EOF
            for((y = 0; y <= "$slave"-1; y++))
            do
               cat <<EOF | mysql -uapollo -pApollo2017!
drop database if exists master_${x}_slave_${y};
EOF
            done
        done
    fi
}

init 4